var validator;
var $problemFeedbackAddForm = $("#problemFeedback-add-form");
$(function () {
    validateRule();
    $("#problemFeedback-add .btn-save").click(function () {
        validator = $problemFeedbackAddForm.validate();
        var flag = validator.form();
        if (flag) {
            $.post(ctx + "problemFeedback/add", $problemFeedbackAddForm.serialize(), function (r) {
                if (r.code === 0) {
                    closeModal();
                    refresh();
                    document.getElementById("problemFeedback-add-form").reset()
                    $('#problemFeedback-add').modal("hide");
                    $MB.n_success(r.msg);
                } else $MB.n_danger(r.msg);
            });
        }
    });





});
// $("#problemFeedback-add .btn-close").click(function () {
//     closeModal();
// });

function problemFeedbackClose() {
    validator.resetForm();
    document.getElementById("problemFeedback-add-form").reset();
    // $("#pic").val("");
    // $('input[type="text"][name="enclosure"]').hidden;
    // document.getElementById("problemFeedback-file").reset();
    $('#problemFeedback-add').modal("hide");
}


function closeModal() {
    $("#problemFeedback-add-button").attr("name", "save");
    $("#problemFeedback-add-modal-title").html('新增字典');
    validator.resetForm();
    $MB.closeAndRestModal("problemFeedback-add");
}

function validateRule() {
    var icon = "<i class='zmdi zmdi-close-circle zmdi-hc-fw'></i> ";
    validator = $problemFeedbackAddForm.validate({
        rules: {
            problemRecord: {
                required: true,
                maxlength: 127
            },
            expectedResults: {
                required: true,
                maxlength: 127
            }
        },
        messages: {
            problemRecord: {
                required: icon + "请输入问题",
                maxlength: icon + "长度不能超过127个字符"
            },
            expectedResults: {
                required: icon + "请输入预期结果",
                maxlength: icon + "长度不能超过127个字符"
            }
        }
    });
}


function upload() {
   $("#file").click();
    $('#file').change(function (e) {
        var fileName = e.target.files[0];//js 获取文件对象
        if (fileName !== undefined) {
            var file_typename = fileName.name.substring(fileName.name.lastIndexOf('.'));
            if (file_typename === '.xlsx' || file_typename === '.xls' || file_typename === '.docx' || file_typename === '.doc' || file_typename === '.wps') {
                $("#filename").css("display", "block");
                $("#filename").val(fileName.name);
                UploadFile(fileName);
            } else {
                console.log("请选择正确的文件类型！")
            }
        } else {
            console.log("请选择正确的文件！")
        }
    })

}


function UploadFile(fileObj) {
    var form = new FormData(); // FormData 对象
    form.append("file", fileObj); // 文件对象
    $.ajax({
        url: ctx + "problemFeedback/upload",                      //url地址
        type: 'POST',                 //上传方式
        data: form,                   // 上传formdata封装的数据
        dataType: 'JSON',
        cache: false,                  // 不缓存
        processData: false,        // jQuery不要去处理发送的数据
        contentType: false,         // jQuery不要去设置Content-Type请求头
        success: function (r) {
            if (r.status) {
                $('input[type="text"][name="enclosure"]').val(r.data.enclosure);
                // $("#filename").css("display", "none");
                form.empty();
                $MB.n_success(r.message);
                refresh();
            } else $MB.n_danger(r.message);
        }
    });
}
